/**
 * 
 */
package com.projectsquirrel;

import java.io.OutputStreamWriter;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

/**
 * @author Sriram
 *
 */
public class StoryUpload extends Activity{
	
	private static final int SELECT_STORY = 1;
	private String selectedFilePath;
    private String filemanagerstring;

    final String TAG = "Project_2_Group_6";

	
	 protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.storyupload);
	        Button storySubmitButton = (Button) findViewById(R.id.storySubmitButton);
	        final EditText storyText = (EditText) findViewById(R.id.storyText);
	        final Context context = getApplicationContext();
		    
	        
	        // capture our View elements
	        final ImageButton settingsIconButton = (ImageButton) findViewById(R.id.settingnfoScreenIcon);
	        final ImageButton infoIconButton = (ImageButton) findViewById(R.id.infoScreenIcon);
	        final Button browseStory = (Button) findViewById(R.id.storyBrowseButton);
	        
	        storySubmitButton.setOnClickListener(new OnClickListener()
		    {
		    	public void onClick(View v)
		    	{
		    		Date today=new Date();
		    		String fName = (today.toString()).concat("_Story");
					try{
						OutputStreamWriter out = new OutputStreamWriter(openFileOutput(fName, 0));
						out.write(storyText.getEditableText().toString());
						Log.i(TAG, storyText.toString());
						out.close();
					}
					catch(Throwable t) {
						Toast.makeText(context, "Exception: "+ t.toString(), 5000).show();
					}
					Toast.makeText(getApplicationContext(),"Story Submitted!",Toast.LENGTH_LONG).show();
					finish();
		    	}
		    });

	        
	        settingsIconButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent i = new Intent(StoryUpload.this, settingsMenu.class);
			        startActivity(i);
				}	    	
		    });
	        
		    infoIconButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent i = new Intent(StoryUpload.this, infoMenu.class);
			        startActivity(i);
				}	    	
		    });
		    
		    browseStory.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// select a file
	                Intent intent = new Intent();
	                intent.setType("image/*");
	                intent.setAction(Intent.ACTION_GET_CONTENT);
	                startActivityForResult(Intent.createChooser(intent,
	                        "Select File"), SELECT_STORY);
				}	    	
		    });
	 }
		    @Override
		    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		        super.onActivityResult(requestCode, resultCode, data);
		        
		        if (resultCode == RESULT_OK) {
		            if (requestCode == SELECT_STORY) {
		                Uri selectedFileUri = data.getData();

		                //OI FILE Manager
		                filemanagerstring = selectedFileUri.getPath();

		                //Story GALLERY
		                selectedFilePath = getPath(selectedFileUri);
		            }
		        }
		    }

	public String getPath(Uri uri) {
		        String[] projection = { MediaStore.Images.Media.DATA };
		        Cursor cursor = managedQuery(uri, projection, null, null, null);
		        if(cursor!=null)
		        {
		            //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
		            //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
		            int column_index = cursor
		            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		            cursor.moveToFirst();
		            return cursor.getString(column_index);
		        }
		        else return null;
		    }
	 
}
